var orderPayDetailApp = angular.module("orderPayDetailApp",[]);
orderPayDetailApp.controller("orderPayDetailController",function($scope){
    //订单价格数据处理，从2处理成2.00格式
	$scope.intToFloat = function(num) {
            num = Math.round(num * 100) / 100;
            var obj = num.toString().split(".");
            if (obj.length == 1) {
                num = num + '.00';
            } else {
                if (obj[1].length == 1) {
                    num = num + '0';
                }
            }
            return num;
            
        }
    //获取本地存储的订单费用明细信息
    var chargeDetail=localStorage.getItem('payDetail');

    //2
    if(chargeDetail){
		$scope.order=JSON.parse(chargeDetail);
		//计算充电费和服务费，优惠
		$scope.chargePrice=0;
		$scope.servicePrice=0;
		$scope.discountPrice=0;
		$scope.discountPriceStr=[];
		$scope.reservationPrice=0;
		$scope.reservationSinglePrice=0;
		$scope.reservationMessage=[];
		$scope.reservationSingleUnit='';
		$scope.orderDetails=$scope.order.orderDetails;
		//过滤出充电列表
		$scope.chargeList=[];
		$scope.chargelen=0;
		$scope.serviceList=[];
		
		angular.forEach($scope.orderDetails, function(data,index,array){
			if(array[index].serviceNo=="100002"){  //充电费
				$scope.chargePrice=parseFloat($scope.chargePrice+array[index].totalPrice);
				$scope.endTime=array[index].endTime.split(" ")[1].substr(0,5);
				$scope.startTime=array[index].startTime.split(" ")[1].substr(0,5);
				if(array[index].message!="[]"){
					$scope.chargeMessage=JSON.parse(array[index].message);
					$scope.chargeVolume=0;
					angular.forEach($scope.chargeMessage,function (data1,index1,array1) {
						$scope.chargeVolume=parseFloat($scope.chargeVolume+array1[index1].num);
						$scope.chargeMessage[index1].rate=$scope.intToFloat(array1[index1].rate);

					});
					$scope.chargeList={
						"endTime":$scope.endTime,
						"realTotalPrice":array[index].realTotalPrice,
						"startTime":$scope.startTime,
						"totalPrice":array[index].totalPrice,
						"chargeMessage":$scope.chargeMessage,
						"chargeVolume":$scope.chargeVolume
					};
				}else{
					$scope.chargeList={
					"endTime":$scope.endTime,
					"realTotalPrice":array[index].realTotalPrice,
					"startTime":$scope.startTime,
					"totalPrice":array[index].totalPrice,
					"chargeMessage":"",
					"chargeVolume":"0"
					};
				}
			}else if(array[index].serviceNo=="100001"){   //服务费
				
				$scope.servicePrice=parseFloat($scope.servicePrice+array[index].totalPrice);
				$scope.serviceendTime=array[index].endTime.split(" ")[1].substr(0,5);
				$scope.servicestartTime=array[index].startTime.split(" ")[1].substr(0,5);
				if(array[index].message!="[]"){
					$scope.serviceMessage=JSON.parse(array[index].message);
					$scope.serviceVolume=0;
					angular.forEach($scope.serviceMessage,function (data1,index1,array1) {
						$scope.serviceVolume=$scope.intToFloat(parseFloat($scope.serviceVolume+array1[index1].num));
						if(array1[index1].paymentMode=="0"){
							$scope.serviceUnit="度";
						}else{
							$scope.serviceUnit="次"
						}
						$scope.serviceMessage[index1].rate=$scope.intToFloat(array1[index1].rate);
					});
					$scope.serviceList={
						"endTime":$scope.serviceendTime,
						"realTotalPrice":array[index].realTotalPrice,
						"startTime":$scope.servicestartTime,
						"totalPrice":array[index].totalPrice,
						"serviceMessage":$scope.serviceMessage,
						"serviceVolume":$scope.serviceVolume,
						"serviceUnit":$scope.serviceUnit
					};
				}else{
					$scope.serviceList={
					"endTime":$scope.serviceendTime,
					"realTotalPrice":array[index].realTotalPrice,
					"startTime":$scope.servicestartTime,
					"totalPrice":array[index].totalPrice,
					"serviceMessage":"",
					"serviceVolume":"",
					"serviceUnit":""
					};
				}
			}else if(array[index].serviceNo=="100003"){   //预约费
				
				$scope.reservationPrice=parseFloat($scope.servicePrice+array[index].totalPrice);
				$scope.reservationMessage=JSON.parse(array[index].message);
				$scope.reservationSinglePrice=$scope.intToFloat($scope.reservationMessage[0].rate);
				if($scope.reservationMessage[0].paymentMode=="0"){
					$scope.reservationSingleUnit="分钟";
				}else{
					$scope.reservationSingleUnit="次";
				}
			}
			$scope.discountPrice=parseFloat($scope.discountPrice)+parseFloat(array[index].totalPrice-array[index].realTotalPrice);
			if(array[index].activityRule!=undefined){
				$scope.discountPriceStr.push(array[index].activityRule);
			}
		});
		$scope.reservationPrice=$scope.intToFloat($scope.reservationPrice);
		$scope.reservationSinglePrice=$scope.intToFloat($scope.reservationSinglePrice);
		$scope.chargePrice=$scope.intToFloat($scope.chargePrice);
		$scope.servicePrice=$scope.intToFloat($scope.servicePrice);
		//判断优惠金额是否有值，有则显示，无则隐藏
		$scope.hasDiscount=false;
		if($scope.discountPrice && $scope.discountPrice>0){
			$scope.hasDiscount=true;
			$scope.discountPrice=$scope.intToFloat($scope.discountPrice);
		}
		
		//2
		//计算预约时长
		$scope.reservationTime="0分钟";
		//$scope.reservationDetail=$scope.order.reservationDetail;
		if($scope.order.reservationDetail!=undefined){
			current=($scope.order.reservationDetail.actualEndTime-$scope.order.reservationDetail.startTime)/1000; //计算当前时间与开始时间的时间差
			var total=2*60*60;
			var leave1=parseInt(current)%(24*3600);
			var sh=Math.floor(leave1/3600)+"小时";
			var leave2=leave1%3600;
			var sm=Math.floor(leave2/60)+"分钟";
			$scope.reservationTime=sh+sm;
		}

	}
})